// pages/addTraveler/index.js
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false, 
    state: false,
    sex: 'men',
    columns: ['请选择', '成人', '老人', '儿童', '婴儿'],
    personType: '1',
    personTypeIndex:1,
    certificates: ['请选择', '身份证', '通行证'],
    certificatesType: '请选择',
    cardTypeIndex: 0,
    userName:'',
    lastName:'',
    firstName:'',
    certificateNo:'',
    passportNo:'',
    phoneNo:'',
    btnName: '确认保存',
    isAdd: true,//是否是新增，false为修改
    id:''
  },

  nameInput: function(e){
    this.setData({
      userName: e.detail.value
    })
  },
  lastNameInput: function (e) {
    this.setData({
      lastName: e.detail.value
    })
  },
  firstNameInput: function (e) {
    this.setData({
      firstName: e.detail.value
    })
  },
  certificateNoInput: function (e) {
    let value = e.detail.value.replace(/[^0-9X]/g, '');
    this.setData({
      certificateNo: value
    })
  },
  passportNoInput: function (e) {
    let value = e.detail.value.replace(/[^0-9a-zA-Z]/g, '');
    this.setData({
      passportNo: value
    })
  },
  phoneNoInput: function (e) {
    let value = e.detail.value.replace(/[^0-9]/g, '');
    this.setData({
      phoneNo: value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let uid = options.id;
    if (!uid){
      return;
    }

    let userInfo = util.isLogin();
    let that = this;
    this.setData({
      id: uid
    });
    util.http({
      url: '/api/miniwechat/center/find_people',
      headers: {
        token: userInfo.token
      },
      data: {
        id: uid,
        user_id: userInfo.user_id
      }
    }).then(res => {
      console.log("addTraveler find_people result------↓↓↓↓")
      console.log(res);
      that.setData({
        userName: res.realname,
        lastName: res.surname,
        firstName: res.name,
        certificateNo: res.card_number,
        passportNo: res.passport_number,
        phoneNo: res.mobile,
        sex: res.gender == 1 ? 'men' : 'women',
        personTypeIndex: res.person_type,
        personType: that.data.columns[res.person_type],
        certificatesType: that.data.certificates[res.card_type],
        cardTypeIndex: res.card_type,
        btnName : '确认修改',
        isAdd: false
      });
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  changeOn: function (e) {
    this.setData({ sex: e.currentTarget.dataset.sex });
  },
  changeType:function(e){
    this.setData({ personType: e.currentTarget.dataset.type, personTypeIndex: e.currentTarget.dataset.type});
  },
  openPop() {
    console.log(this.data.columns);
    this.setData({ show: true });
  },
  openCertPop () {
    this.setData({ state: true });
  },
  onClose() {
    this.setData({ show: false, state: false });
  },
  onCancel() {
    this.setData({ show: false, state: false });
  },
  onConfirm() {
    this.setData({ show: false, state: false });
  },

  onChange(e){
    let selectValue = e.detail.value;
    let selectIndex = e.detail.index;
    if (selectIndex < 1) {
      return;
    }
    if (this.data.columns.indexOf(selectValue) > 0) {
      this.setData({
        personType: selectValue,
        personTypeIndex: selectIndex
      });
    }
    else if (this.data.certificates.indexOf(selectValue) > 0){
      this.setData({
        certificatesType: selectValue,
        cardTypeIndex: selectIndex
      });
    }
  },

  AddTraveler(){
    if (this.data.userName == '') {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    if (this.data.personTypeIndex == 0) {
      wx.showToast({
        title: '请选择人员类型',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    if (this.data.cardTypeIndex == 0) {
      wx.showToast({
        title: '请选择证件类型',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    if (this.data.certificatesType == '身份证') {
      if (this.data.certificateNo == '') {
        wx.showToast({
          title: '请输入证件号码',
          icon: 'none',
          duration: 2000
        });
        return;
      }
      if (this.data.certificateNo != '' && !util.IdentityCodeValid(this.data.certificateNo)) {
        wx.showToast({
          title: '身份证号输入不正确',
          icon: 'none',
          duration: 2000
        });
        return;
      }
    }
    if (this.data.phoneNo == '') {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    // if (this.data.userName == '' || this.data.lastName == '' || this.data.firstName == '' ||
    //   this.data.personTypeIndex == 0 || this.data.cardTypeIndex == 0 
    // ) {//this.data.certificateNo == '' || this.data.passportNo == '' || this.data.phoneNo == ''   可以为空
    //   wx.showToast({
    //     title: '请完善您的信息',
    //     icon: 'none',
    //     duration: 2000
    //   });
    //   return;
    // }

    

    // if (this.data.passportNo != '' && !(/^[a-zA-Z0-9]{5,17}$/.test(this.data.passportNo))){
    //   wx.showToast({
    //     title: '护照号码输入不正确',
    //     icon: 'none',
    //     duration: 2000
    //   });
    //   return;
    // }

    if (this.data.phoneNo != '' && !(/^1\d{10}$/.test(this.data.phoneNo))){
      wx.showToast({
        title: '手机号码输入不正确',
        icon: 'none',
        duration: 2000
      });
      return;
    }

    let userInfo = util.isLogin();
    let addUrl = '/api/miniwechat/center/add_people';
    if (!this.data.isAdd){
      addUrl = '/api/miniwechat/center/edit_people';
    }
    util.http({
      url: addUrl,
      method: 'POST',
      headers:{
        token: userInfo.token
      },
      data: {
        user_id: userInfo.user_id,
        realname: this.data.userName,
        surname: this.data.lastName,
        name: this.data.firstName,
        gender: this.data.sex == 'men' ? 1 : 2,
        person_type: this.data.personTypeIndex,
        card_type: this.data.cardTypeIndex,
        card_number: this.data.certificateNo,
        passport_number: this.data.passportNo,
        mobile: this.data.phoneNo,
        id: this.data.id
      }
    }).then(res => {
      console.log("AddTraveler result------↓↓↓↓")
      console.log(res);
      wx.navigateBack({
        delta: 1
      })
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  }
})